<body class="iframe">
    <div class="cate">
        <div class="frame-header">
            <h2><i class="layui-icon">&#xe705;</i>设置</h2>
        </div>
        <?php if ($userinfo['level'] >= 99): ?>
            <h4 class="layui-bg-dark frame-title"><i class="layui-icon">&#xe65f;</i>添加邮件服务器</h4>
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-inline">
                        <select name="department" lay-verify="required|number">
                            <option value="0">个人用户</option>
                            <?php foreach ($deparment as $rows): ?>
                                <option value="<?= $rows['id'] ?>"><?= $rows['department_name'] ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">发送接口</label>
                    <div class="layui-input-inline">
                        <input type="text" name="server_address" required lay-verify="required" placeholder="请填写接口地址" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮件host</label>
                    <div class="layui-input-inline">
                        <input type="text" name="server_host" required lay-verify="required" placeholder="请填写邮件host" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">发件地址</label>
                    <div class="layui-input-inline">
                        <input type="text" name="server_sender" required lay-verify="required" placeholder="请填写发件地址" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="addcate">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        <?php endif; ?>
        <h4 class="layui-bg-dark frame-title"><i class="layui-icon">&#xe65f;</i>邮件服务器列表</h4>
        <div class="data-table">
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>id</th>
                        <th>所属部门</th>
                        <th>发送接口</th>
                        <th>邮件host</th>
                        <th>发件地址</th>
                        <th>添加时间</th>
                        <th>修改时间</th>
                        <th>今日使用量</th>
                        <th>累计使用量</th>
                        <th>状态</th>
                        <?php if ($userinfo['level'] >= 99): ?>
                            <th>操作</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
    <div id="edit">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">发送接口</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="tid" class="typeid"/>
                    <input type="text" name="server_address" required lay-verify="required" placeholder="请填写接口地址" autocomplete="off" class="layui-input server_address">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">host地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="server_host" required lay-verify="required" placeholder="请填写host地址" autocomplete="off" class="layui-input server_host">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发件邮箱</label>
                <div class="layui-input-inline">
                    <input type="text" name="server_sender" required lay-verify="required" placeholder="请填写发件邮箱" autocomplete="off" class="layui-input server_sender">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="updatecate">提交修改</button>
                </div>
            </div>
        </form>
    </div>
</body>
<script>

    layui.use(['form', 'layer'], function () {
        var form = layui.form, layer = layui.layer;
        getTypeList();

        //删除分类
        $(document).on("click", ".del", function () {
            let url = '<?= site_url('home/del_mailserver') ?>';
            let id = getId($(this));
            console.log(id);
            let jsonData = {
                'id': id
            };
            request(url, jsonData, getTypeList, layer);
        })
        //编辑分类
        $(document).on("click", ".edit-btn", function () {
            let tid = getId($(this));
            let server_address = $(this).parents("tr").find('.server_address').text();
            let server_host = $(this).parents("tr").find('.server_host').text();
            let server_sender = $(this).parents("tr").find('.server_sender').text();
            $("#edit form .server_address").val(server_address);
            $("#edit form .server_host").val(server_host);
            $("#edit form .server_sender").val(server_sender);
            $("#edit form .typeid").val(tid);
            layer.open({
                'type': 1,
                'title': '修改邮件服务器信息',
                'content': $('#edit'),
                'area': ['600px', '400px']
            });
        })
        //修改分类
        form.on('submit(updatecate)', function (data) {
            let url = '<?= site_url('home/update_mailserver') ?>';
            let server_address = data.field.server_address;
            let server_host = data.field.server_host;
            let server_sender = data.field.server_sender;
            let tid = data.field.tid;
            let jsonData = {
                'server_address': server_address,
                'server_host': server_host,
                'server_sender': server_sender,
                'id': tid
            };
            request(url, jsonData, getTypeList, layer);
            return false;
        });
        //添加分类
        form.on('submit(addcate)', function (data) {
            let url = '<?= site_url('home/add_mailserver') ?>';
            let server_address = data.field.server_address;
            let server_host = data.field.server_host;
            let server_sender = data.field.server_sender;
            let department_id = data.field.department;
            let jsonData = {
                'department_id':department_id,
                'server_address': server_address,
                'server_host': server_host,
                'server_sender': server_sender
            };
            console.log()
            request(url, jsonData, getTypeList, layer);
            return false;
        });

    });

</script>
<script>
    function getTypeList() {
        let url = '<?= site_url("home/get_mailserver") ?>';
        $.get(url, function (redata) {
            let str = '';

            for (x in redata.Mailserverlist) {
                let num = parseInt(x) + 1;
                let enablestr = '';
                let operatestr = '';
                if (redata.Mailserverlist[x].enable == 1) {
                    enablestr = '<span class="layui-badge">可用</span>';
                } else {
                    enablestr = '<span class="layui-badge layui-bg-gray">过期</span>';
                }
<?php if ($userinfo['level'] >= 99): ?>
                    operatestr = '<td><div class="layui-btn-group"><button class="layui-btn layui-btn-normal layui-btn-xs edit-btn"><i class="layui-icon">&#xe642;</i>编辑</button><button class="layui-btn layui-btn-danger layui-btn-xs del" ><i class="layui-icon">&#xe640;</i>删除</button></div></td>';
<?php endif; ?>

                str += '<tr tid="' + redata.Mailserverlist[x].id + '"><td>' + num + '</td><td>'+redata.Mailserverlist[x].department_name+'</td><td class="server_address">' + redata.Mailserverlist[x].server_address + '</td><td class="server_host">' + redata.Mailserverlist[x].server_host + '</td><td class="server_sender">' + redata.Mailserverlist[x].server_sender + '</td><td>' + redata.Mailserverlist[x].create_time + '</td><td>' + redata.Mailserverlist[x].update_time + '</td><td>' + redata.Mailserverlist[x].today_usage + '</td><td>' + redata.Mailserverlist[x].total_usage + '</td><td>' + enablestr + '</td>' + operatestr + '</tr>'
            }
            $(".data-table tbody").html(str);
        }, 'json');

    }
</script>

</html>